package com.xbongbong.paas.script.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.script.pojo.dto.ProductDTO;
import com.xbongbong.saas.service.RepairWarehouseExplainService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author: wujian
 * @create: 2021-01-28 17:15
 * @description: 仓库新增子表单字段
 **/
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/script/warehouse")
public class RepairWarehouseExplainController {
    @Resource
    RepairWarehouseExplainService repairWarehouseExplainService;

    @RequestMapping(value = "/repairWarehouseExplain", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public String repairWarehouseExplain(@RequestBody @Valid ProductDTO productDTO, BindingResult br) throws XbbException {
        XbbResponse<String> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                String scriptVO = repairWarehouseExplainService.repairWarehouseExplain(productDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
